package datamodels

import (
	"github.com/spf13/cast"
	"micro-oauth/utils"
)

type PassWordRule struct {
	Id 				uint64 					`json:"id"`
	UserId    		uint64					`json:"user_id"`
	LoginStatus		int						`json:"login_status"`
	PassWordForm	map[string]interface{}	`json:"passwd_form"`
	PassWordLength  map[string]interface{}	`json:"passwd_length"`
	PassWordExpire	string		    		`json:"passwd_expire"`
	ExpireDay		int 					`json:"expire_day"`
	ExpireMonth		int 					`json:"expire_month"`
}

func (rule *PassWordRule)Verify(password string) bool{
	if len(rule.PassWordLength) > 0 {
		lengthRule := rule.PassWordLength
		if startStr,ok := lengthRule["start"];ok{
			start,err := cast.ToIntE(startStr);if err != nil{
				return true
			}
			if start > len(password){
				return false
			}
		}

		if endStr,oke := lengthRule["end"];oke{
			end,err := cast.ToIntE(endStr);if err != nil{
				return true
			}
			if end < len(password){
				return false
			}
		}
	}

	if len(rule.PassWordForm) > 0 {
		formRule := rule.PassWordForm
		if symbolS,ok := formRule["symbol"];ok{
			if symbol,okc := symbolS.(bool);okc {
				if symbol {
					if !utils.ContainsSymbol(password) {
						return false
					}
				}
			}
		}

		if numberS,ok := formRule["number"];ok{
			if number,okc := numberS.(bool);okc {
				if number {
					if !utils.ContainsNumber(password) {
						return false
					}
				}
			}
		}

		if bigS,ok := formRule["letter"];ok{
			if big,okc := bigS.(bool);okc {
				if big {
					if !utils.ContainsLetter(password) {
						return false
					}
				}
			}
		}

		if bigS,ok := formRule["big_letter"];ok{
			if big,okc := bigS.(bool);okc {
				if big {
					if !utils.ContainsBigLetter(password) {
						return false
					}
				}
			}
		}

	}


	return true
}
